local lingjue = fk.CreateSkill{
  name = "hanqing__lingjue",
  tags = { Skill.Lord, Skill.Compulsory },
  dynamic_desc = function(self, player)
    if player:getMark("hanqing__lingjue") ~= 0 then
      return "hanqing__lingjue_inner:"..table.concat(table.map(player:getTableMark(self.name), function (s)
        return Fk:translate(s)
      end), "")
    end
  end,
}

Fk:loadTranslationTable{
  ["hanqing__lingjue"] = "凌绝",
  [":hanqing__lingjue"] = "主公技，锁定技，其他『齐』势力角色的明置牌均视为♠。场上每失去一个势力，你将另一个势力并入『 』。",

  [":hanqing__lingjue_inner"] = "主公技，锁定技，其他『{1}』势力角色的明置牌均视为♠。"..
  "场上每失去一个势力，你将另一个势力并入『 』。",

  ["#hanqing__lingjue-choice"] = "凌绝：选择吞并一个势力！",
}

local DIY = require "packages/diy_utility/diy_utility"

lingjue:addEffect(fk.Deathed, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(lingjue.name) and
      table.every(player.room.alive_players, function (p)
        return p.kingdom ~= target.kingdom
      end) and
      table.find(player.room.alive_players, function (p)
        return p.kingdom ~= "qi" and not table.contains(player:getTableMark(lingjue.name), p.kingdom)
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local kingdoms = player:getTableMark(lingjue.name)
    if #kingdoms == 0 then kingdoms = {"qi"} end
    local all_kingdoms = {}
    for _, p in ipairs(room.players) do
      if p.kingdom ~= target.kingdom and not table.contains(kingdoms, p.kingdom) then
        table.insertIfNeed(all_kingdoms, p.kingdom)
      end
    end
    local choice = room:askToChoice(player, {
      choices = all_kingdoms,
      skill_name = lingjue.name,
      prompt = "#hanqing__lingjue-choice",
    })
    table.insert(kingdoms, choice)
    room:setPlayerMark(player, lingjue.name, kingdoms)
  end,
})

lingjue:addEffect("filter", {
  mute = true,
  card_filter = function(self, to_select, player, isJudgeEvent)
    return table.contains(DIY.getShownCards(player), to_select.id) and
      table.find(Fk:currentRoom().alive_players, function (p)
        return p ~= player and p:hasSkill(lingjue.name) and
          (player.kingdom == "qi" or table.contains(p:getTableMark(lingjue.name), player.kingdom))
      end)
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard(to_select.name, Card.Spade, to_select.number)
  end,
})

lingjue:addAcquireEffect(function (self, player)
  player.room:addSkill("#CenterArea")
end)

lingjue:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, lingjue.name, 0)
end)

return lingjue
